<?php

namespace app\admin\model;

use think\Model;

class Order extends Model
{
    protected $table = 'k_order';

    public function getGoodsListAttr($value)
    {
        $field = "rec_id as id,o.goods_id as goodsId, o.goods_name as goodsName,g.original_img as originalImg,
        o.goods_num as goodsNum,o.goods_price as goodsPrice,(o.goods_price * o.goods_num) as totalPrice,o.spec_id as specId,o.prom_id as businessId,is_send as isSend";
        return model("OrderGoods")->alias("o")->field($field)->join("k_goods g", "g.goods_id = o.goods_id")->where("order_id", $value)->select();
    }

    public function getPayTimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d h:i:s', $value) : "";
    }

    public function getAddTimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d h:i:s', $value) : "";
    }

    public function getShippingTimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d h:i:s', $value) : "";
    }

    public function getConfirmTimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d h:i:s', $value) : "";
    }

    public function getTotalNumAttr($value)
    {
        return db("goods")->alias("g")->join("k_order_goods o", "g.goods_id = o.goods_id")->where("order_id", $value)->sum("o.goods_num");
    }

    public function getTotalPriceAttr($value)
    {
        return db("goods")->alias("g")->join("k_order_goods o", "g.goods_id = o.goods_id")->where("order_id", $value)->sum("o.goods_price * o.goods_num");
    }
}